package com.ruoyi.system.controller;

import com.aliyun.oss.OSS;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.TbDriver;
import com.ruoyi.system.domain.vo.TbDriverVo;
import com.ruoyi.system.service.DriverService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/user/driver")
public class DriverController {

    @Resource
    private DriverService driverService;

    @GetMapping("/getDriverInfo/{driverId}")
    public AjaxResult getDriverInfo(@PathVariable("driverId") Integer driverId){
        TbDriverVo tbDriverVo =driverService.getDriverInfo(driverId);
        return AjaxResult.success(tbDriverVo);
    }

    @PutMapping("/updateDriverInfo")
    public AjaxResult updateDriverInfo(@RequestBody TbDriver tbDriver){
        driverService.updateDriverInfo(tbDriver);
        return AjaxResult.success();
    }

    @Resource
    private OSS ossClient;

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        String substring = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileName = UUID.randomUUID().toString().replace("-", "") + "." + substring;
        ossClient.putObject("lujintao-bucket",fileName,file.getInputStream());
        return AjaxResult.success("https://lujintao-bucket.oss-cn-beijing.aliyuncs.com/"+fileName);
    }


}
